﻿using System;
using System.Collections.Generic;
using System.Text;
using Ganosoft.DynamicProxy;

namespace GanoESB.Business
{
    internal class ServiceInterceptor : IInterceptor
    {
        private ServiceController controller;

        public ServiceInterceptor(ServiceController controller)
        {
            this.controller = controller;
        }

        #region IInterceptor

        public object Intercept(InvocationInfo invocation)
        {
            ServiceInvocation inv = new ServiceInvocation(controller, invocation);
            try
            {
                inv.ServiceCall.ExecuteTime = DateTime.Now;
                inv.ServiceCall.ServiceMethod = invocation.TargetMethod.Name;

                return inv.InvokeNext();
            }
            catch (Exception ex)
            {
                inv.ServiceCall.ErrorMsg = ex.Message;
            }
            finally
            {
                inv.ServiceCall.FinishTime = DateTime.Now;
            }

            return null;
        }

        #endregion

    }
}
